package 剑指Offer.easy;

import java.util.PriorityQueue;
import java.util.Queue;

public class 最小的k个数 {
    class Solution {
        public int[] getLeastNumbers(int[] arr, int k) {
            if (arr == null || arr.length == 0 || k == 0) return new int[0];
            Queue<Integer> priorityQueue = new PriorityQueue<>((i, j) -> j - i);
            for (int i = 0; i < arr.length; i++) {
                if (i < k) {
                    priorityQueue.add(arr[i]);
                } else if (arr[i] < priorityQueue.peek()) {
                    priorityQueue.poll();
                    priorityQueue.add(arr[i]);
                }
            }
            int[] res = new int[k];
            return priorityQueue.stream().mapToInt(Integer::intValue).toArray();
        }
    }
}
